package com.leetcode.offer.chapter5;

import com.leetcode.basic.TreeNode;

/**
 * @author Dennis Li
 * @date 2020/7/15 17:21
 */
public class BSTkthLargest_54 {

    int count = 0, ans = 0;

    public int kthLargest(TreeNode root, int k) {
        dfs(root, k);
        return ans;
    }

    public void dfs(TreeNode root, int m) {
        // 从右到左进行遍历
        if (root.right != null) dfs(root.right, m);
        // 用一个结点来代替遍历的过程
        if (++count == m) {
            ans = root.val;
            return;
        }
        if (root.left != null) dfs(root.left, m);
    }

}
